/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dp1.titandevelop.titano.view.prod;

import dp1.titandevelop.titano.bean.Mensajes;
import dp1.titandevelop.titano.persistent.Asignacion;
import dp1.titandevelop.titano.persistent.EmpleadoXProcesoTurno;
import dp1.titandevelop.titano.persistent.PadreProduccion;
import dp1.titandevelop.titano.persistent.Produccion;
import dp1.titandevelop.titano.persistent.Receta;
import dp1.titandevelop.titano.service.EmpleadoXProcesoTurnoService;
import dp1.titandevelop.titano.service.ProduccionService;
import dp1.titandevelop.titano.service.ValidacionService;
import dp1.titandevelop.titano.view.MensajeError;
import java.awt.Dimension;
import java.awt.Frame;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author chrestean
 */
public class ModulosT_PRO_ProduccionEdita extends javax.swing.JInternalFrame {

    ProduccionService s;
    ArrayList<Produccion> listaProduccion;
    PadreProduccion p;
    EmpleadoXProcesoTurnoService sept;
    ArrayList<EmpleadoXProcesoTurno> ept= new ArrayList<EmpleadoXProcesoTurno>();
    /**
     * Creates new form TitanoInteralSegADMUser
     */
    //Refrescar la tabla de productos
    public void RefrescarTabla() {
        
        AbstractTableModel aux = (AbstractTableModel) tblProduccion.getModel();
           // this.inhabilitaBotones();
        aux.fireTableDataChanged();
        }
    
    public void AgregaProduccion(Asignacion a, float prod, float rotura ) {
        Produccion p = new Produccion ();
        p.setToAsignacion(a);
        p.setProductividad(prod);
        p.setRotura(rotura);        
        //calcular beneficio y costo.       
        p.setBeneficio(prod*a.getToProducto().getBeneficio());
        p.setCosto((prod+rotura)*a.getToProducto().getCosto());
        listaProduccion.add(p);        
        EmpleadoXProcesoTurno e1= new EmpleadoXProcesoTurno();
        e1=this.sept.buscarUnEmpleadoXProceso(a.getToEmpleado().getIdempleado(), a.getToMaquina().getToProceso().getIdproceso(), a.getToTurno().getIdturno());
        ept.add(e1);
    }

    private AbstractTableModel modelo = new AbstractTableModel() {
        public int getRowCount() {
            if (listaProduccion!=null)
            return listaProduccion.size();
            else return 0;
        }

        public int getColumnCount() {
        return 7;
        }

        String [] titles = {"Operario","Turno","Proceso","Prod. Real", "Rot. Real", "Prod. Teorica", "Rot. Teorica"};

        @Override
        public String getColumnName(int col){
        return titles[col];
        }

        public Object getValueAt(int rowIndex, int columnIndex) {
        //String res=null;
        Produccion p=listaProduccion.get(rowIndex);
            switch (columnIndex){
            case 0: return p.getToAsignacion().getToEmpleado().getNombres()+ " "+p.getToAsignacion().getToEmpleado().getApellidop();
            case 1: return p.getToAsignacion().getToTurno().getDescripcion();
            case 2: return p.getToAsignacion().getToMaquina().getToProceso().getDescripcion();
            case 3: return p.getProductividad();
            case 4: return p.getRotura();
            case 5: return ept.get(rowIndex).getProductividad();
            case 6: return ept.get(rowIndex).getRotura();
            }
            return null;
            } 
    };
    public ModulosT_PRO_ProduccionEdita(PadreProduccion p) {
        initComponents();
        ValidacionService vs = new ValidacionService();
        this.sept = new EmpleadoXProcesoTurnoService();
        ept= new ArrayList<EmpleadoXProcesoTurno>();
        vs.NoEscribe(this.txtFecha);
        Date d=new Date();
        this.p = p;
        txtDescripcion.setText(p.getDescripcion());
        txtFecha.setDate(new Date(d.getTime()));
        txtDescripcion.setEnabled(false);
        txtFecha.setEnabled(false);
        s = new ProduccionService();
        listaProduccion = s.SacaDetalle(p.getIdpadreproduccion());
        for (int i=0; i< listaProduccion.size(); i++)
        {
            EmpleadoXProcesoTurno ep= this.sept.buscarUnEmpleadoXProceso(listaProduccion.get(i).getToAsignacion().getToEmpleado().getIdempleado(), listaProduccion.get(i).getToAsignacion().getToMaquina().getToProceso().getIdproceso(),listaProduccion.get(i).getToAsignacion().getToTurno().getIdturno());
            ept.add(ep);
        }
        this.tblProduccion.setModel(this.modelo); 
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        lblFecha = new javax.swing.JLabel();
        txtFecha = new org.jdesktop.swingx.JXDatePicker();
        jLabel1 = new javax.swing.JLabel();
        txtDescripcion = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblProduccion = new javax.swing.JTable();
        jButton2 = new javax.swing.JButton();

        setResizable(true);
        setTitle("Ver Detalle De Producción");
        setPreferredSize(new java.awt.Dimension(616, 385));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Filtros de Búsqueda"));
        jPanel1.setFont(new java.awt.Font("Tekton Pro", 0, 11)); // NOI18N

        lblFecha.setText("Fecha");

        jLabel1.setText("Descripción");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblFecha)
                .addGap(18, 18, 18)
                .addComponent(txtFecha, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(99, 99, 99)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtDescripcion, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblFecha)
                    .addComponent(txtFecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(txtDescripcion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(20, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Productos"));
        jPanel2.setFont(new java.awt.Font("Tekton Pro", 0, 11)); // NOI18N

        tblProduccion.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Proceso", "Operario", "Producto", "Produccion", "Merma"
            }
        ));
        jScrollPane1.setViewportView(tblProduccion);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/cancelar.png"))); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jButton2)))
                .addGap(10, 10, 10))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton2)
                .addGap(0, 11, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        ModulosT_PRO.admProduccion = new ModulosT_PRO_AdmPro();
        this.getParent().add(ModulosT_PRO.admProduccion);

        Dimension desktopSize = this.getParent().getSize();
        Dimension jInternalFrameSize = ModulosT_PRO.admProduccion.getSize();
        ModulosT_PRO.admProduccion.setLocation((desktopSize.width - jInternalFrameSize.width) / 2,
            (desktopSize.height - jInternalFrameSize.height)/4 );
        ModulosT_PRO.admProduccion.show();
         ModulosT_PRO.ventanaProduccionEdita=null;
        this.dispose();
         
    }//GEN-LAST:event_jButton2ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblFecha;
    private javax.swing.JTable tblProduccion;
    private javax.swing.JTextField txtDescripcion;
    private org.jdesktop.swingx.JXDatePicker txtFecha;
    // End of variables declaration//GEN-END:variables

}
